<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="企业名称">
        <el-select
          v-model="queryParams.deptId"
          filterable
          placeholder="请选择企业名称"
          clearable
        >
          <el-option
            v-for="item in companyOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-card class="box-card" v-for="(item, index) of infoList" :key="index">
      <template slot="header">
        <div class="infos">
          <div style="width: 50%; height: 100%">
            企业信息
            <el-button
              type="primary"
              plain
              icon="el-icon-edit"
              size="mini"
              @click="goToDetDetail(item)"
              v-hasPermi="['system:info:edit']"
              style="margin-left: 10px"
              >基础情况</el-button
            >
            <!-- <el-button
              plain
              icon="el-icon-edit"
              size="mini"
              @click="handleUpdate"
              v-hasPermi="['system:info:edit']"
              >查看基础情况</el-button
            > -->
            <!-- <el-button
              plain
              icon="el-icon-edit"
              size="mini"
              @click="handleBegin(item)"
              >开始测评问卷</el-button
            > -->
            <!-- v-if="form.diagnosisId" -->

            <el-button
              plain
              icon="el-icon-edit"
              size="mini"
              @click="handleBeginxc(item)"
              >测评现场诊断</el-button
            >
            <el-button
              v-if="item.leaderId == $store.state.user.userId"
              plain
              icon="el-icon-edit"
              size="mini"
              @click="handleBeginxczd(item)"
              >开始诊断</el-button
            >

            <div class="detail"></div>
          </div>
          <div
            style="
              width: 50%;
              height: 100%;
              display: flex;
              justify-content: flex-end;
            "
          >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateUser(item)"
              v-hasPermi="['system:info:edit']"
              >编辑用户</el-button
            >
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(item)"
              v-hasPermi="['system:info:edit']"
              >修改</el-button
            > -->
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(item)"
              v-hasPermi="['system:info:remove']"
              >删除</el-button
            >
          </div>
        </div>
      </template>
      <el-descriptions style="width: 50%">
        <el-descriptions-item label="企业名">{{
          item.deptName
        }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{
          item.phone
        }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{
          item.email
        }}</el-descriptions-item>
        <el-descriptions-item label="负责人">
          <el-tag size="small">{{ item.leader }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="组长">
          <el-tag size="small">{{ item.leaderName }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="组长电话">{{
          item.leaderPhone
        }}</el-descriptions-item>
        <!-- <el-descriptions-item label="联系地址"
          >江苏省苏州市吴中区吴中大道 1188 号</el-descriptions-item
        > -->
      </el-descriptions>
      <div style="width: 50%" class="buttonArea">
        <div
          class="statusButton"
          v-for="dict in dict.type.work_note"
          :key="dict.value"
        >
          {{ dict.label }}

          <div class="smallButton" @click="showAA(dict, item, dict.value)">
            {{
              item.diagnosePlan &&
              JSON.parse(item.diagnosePlan).filter(
                (item) => item.workNote == dict.value
              )
                ? JSON.parse(item.diagnosePlan).filter(
                    (item) => item.workNote == dict.value
                  )[0]
                  ? noteStatus.filter(
                      (sta) =>
                        sta.value ==
                        JSON.parse(item.diagnosePlan).filter(
                          (item) => item.workNote == dict.value
                        )[0].noteStatus
                    )[0] &&
                    noteStatus.filter(
                      (sta) =>
                        sta.value ==
                        JSON.parse(item.diagnosePlan).filter(
                          (item) => item.workNote == dict.value
                        )[0].noteStatus
                    )[0].label
                  : "未开始"
                : "未开始"
            }}
          </div>
        </div>
        <!-- <div
          class="statusButton"
          v-for="dict in dict.type.work_note"
          :key="dict.value"
        >
          {{ dict.label }}
          <div class="smallButton" @click="showAA(dict, item, dict.value)">
            已完成
          </div>
        </div> -->
      </div>
    </el-card>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改诊断企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业" required>
          <el-select
            v-model="form.deptId"
            filterable
            allow-create
            default-first-option
            placeholder="请选择需要诊断的企业"
          >
            <el-option
              v-for="item in companyOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组长" prop="groupLeader">
          <el-select
            v-model="form.groupLeader"
            placeholder="请选择组长"
            @change="getUser"
          >
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组员" prop="groupMembers">
          <el-select
            v-model="form.groupMembers"
            multiple
            placeholder="请选择组员"
            @change="getUsers"
          >
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <user-drawer
      :title="user_title"
      :detData="deptData"
      :isActive="user_open"
      width="40%"
      @closeDrawer="closeDra"
      @okDrawer="okDra"
    />
    <statusDrawer
      :title="'文件提交'"
      v-if="status_open"
      :isOpen="status_open"
      :paneValue="paneValue"
      width="50%"
      @closeDraw="closeDraw"
      @okDraw="okDraw"
      :deptDatas="deptDatass"
    />
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  getLeaderInfo,
  listUser,
} from "@/api/system/info";
import { listTask } from "@/api/system/diagnosistask";
import userDrawer from "./userDrawer.vue";
import statusDrawer from "./statusDrawer.vue";

export default {
  components: { userDrawer, statusDrawer },
  name: "Expertdiagnosis",
  dicts: ["work_note", "note_status"],
  data() {
    return {
      isLeader: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 诊断企业表格数据
      companyOptions: [],
      infoList: [],
      userList: [],

      groupLeader: [],
      groupMembers: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      user_title: "",
      // 是否显示弹出层
      user_open: false,
      status_open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "诊断企业名称不能为空", trigger: "blur" },
        ],
      },
      // 诊断企业信息
      deptData: 0,
      //文件上传企业参数
      deptDatass: {},
      //tab索引
      paneValue: "0",
    };
  },
  created() {
    console.log(
      this.$store.state.user.userId,
      this.dict.type.note_status,
      this.noteStatus,
      "dict.type"
    );
    this.getList();
    this.getUserList();
    this.getCompanyList();
  },
  computed: {
    noteStatus() {
      return this.dict.type.note_status;
    },
    num(item) {
      return JSON.parse(item.diagnosePlan).filter(
        (item) => item.workNote == dict.value
      )[0].noteStatus;
    },
  },
  methods: {
    showAA(dict, item, value) {
      console.log(dict, "遍历数据");
      console.log(item, "父传子企业数据");
      this.status_open = true;
      this.deptDatass = item;
      this.paneValue = String(value - 1);
    },
    /** 关闭抽屉 */
    closeDra() {
      this.user_open = false;
    },
    closeDraw() {
      this.status_open = false;
      this.resetQuery();
    },
    /** 关闭抽屉 */
    okDra() {
      this.user_open = false;
      this.resetQuery();
    },
    okDraw() {
      this.status_open = false;
      this.resetQuery();
    },
    /** 查询诊断企业列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
        // getLeaderInfo(response.rows[0]?.deptId).then((response) => {
        //   console.log(response, ".then((response) =>");
        //   this.isLeader = response.data;
        // });
      });
    },
    // 查询诊断企业详细情况跳转新页面
    goToDetDetail(item) {
      console.log(item, "列表数据");
      this.$router.push({
        path: "/expert/enterpriseDetails",
        query: { id: item.deptId },
      });
    },
    /** 查询用户列表 */
    getUserList() {
      listUser().then((response) => {
        this.userList = response.data;
        console.log(this.userList, "this.userList");
      });
    },

    /** 查询诊断任务列列表 */
    getCompanyList() {
      this.loading = true;
      this.companyOptions = [];
      listTask(this.queryParams).then((response) => {
        this.taskList = response.tableInfo.rows;
        console.log(response.tableInfo, "response.tableInfo");
        this.companyNameMap = response.companyNameMap;
        for (let item in this.companyNameMap) {
          this.companyOptions.push({
            value: item,
            label: this.companyNameMap[item],
          });
        }
        this.loading = false;
      });
    },

    getUser(val) {
      this.groupLeader = [val];
    },
    getUsers(val) {
      this.groupMembers = val;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.deptId = undefined;

      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加诊断企业";
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const infoId = row.infoId || this.ids;
    //   getInfo(infoId).then((response) => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改诊断企业";
    //   });
    // },

    /** 编辑用户 */
    handleUpdateUser(row) {
      console.log(row);
      this.deptData = row.deptId;
      this.user_open = true;
      this.user_title = "编辑企业用户";
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let sysCompanyMembers = [];
          this.groupLeader.forEach((item) => {
            sysCompanyMembers.push({
              deptId: this.form.deptId,
              userId: item,
              memberType: 1,
            });
          });
          this.groupMembers.forEach((item) => {
            sysCompanyMembers.push({
              deptId: this.form.deptId,
              userId: item,
              memberType: 2,
            });
          });
          let data = {
            deptId: this.form.deptId,
            sysCompanyMembers,
          };

          console.log(data);
          addInfo(data).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row);
      const infoIds = row.deptId;
      this.$modal
        .confirm('是否确认删除诊断企业编号为"' + infoIds + '"的数据项？')
        .then(function () {
          console.log(infoIds, "infos");
          return delInfo(infoIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },

    //测评问卷部分
    handleBegin(item) {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({
        name: "showQuestionnaire",
        query: { naireId: 102, domain: 56, compId: item.deptId },
        //现场 query: { naireId: 103, domain: 12, compId: item.deptId },
      });
    },

    //测评问卷部分
    handleBeginxc(item) {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({
        name: "showQuestionnaire",
        query: {
          naireId: 103,
          domain: 12,
          compId: item.deptId,
          isDiagnosis: 1,
        },
      });
    },
    handleBeginxczd(item) {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({
        name: "showQuestionnairexczd",
        query: {
          naireId: 103,
          domain: 12,
          compId: item.deptId,
          isDiagnosis: 1,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.box-card {
  margin: 15px 0;
}
::v-deep .el-descriptions__header {
  border-bottom: 1px solid #ccc;
  padding-bottom: 10px;
}
.infos {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
::v-deep .el-card__body {
  display: flex;
}
.buttonArea {
  display: flex;
  flex-wrap: wrap;
  .statusButton {
    width: 33%;
    color: #000;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    // border: 1px solid red;
    .smallButton {
      margin-left: 5px;
      width: 40%;
      color: green;
      border: 1px solid green;
      border-radius: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
    }
  }
}
</style>
